<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Item.php");
require_once("../../data/Comment.php");
require_once("Mail.php");
require_once("Mail/mime.php");

/**
 * Display the form which allow to add a comment to an action
 * @param Item $p_record
 */
function addComment($p_record){
	?>
	<a href="comment_action.php?id=<?=base64_encode($p_record->getId()) ?>">Comment</a>
	<?php
}

/**
 * Display the specified action
 * @param Item $p_record
 */
function display($p_record){
	?>
	- <a href="display_action.php?id=<?=base64_encode($p_record->getId()) ?>">Display</a>
	<?php
}

/**
 * Display an HTML representation of an Item which is an action
 * @param Item $p_action an Item instance
 */
function displayAction($p_action){
?>
<p>
<b>Activity:</b>
<?=$p_action->getActivityLabel() ?>
</p>
<p>
<b>Target date:</b>
<?=$p_action->getTargetDate() ?>
</p>
<b>Description:</b>
<div style="border:1px solid;background-color: white;padding: 5px"><?=$p_action->getDescription() ?></div>
<br/>
<table width="100%">
<tr>
<td>
<b>Created</b>: <?=$p_action->getCreated() ?><br/>
<b>By</b>: 
	<?php
		$l_creatingUser=$p_action->getCreatingUser_id_Ref();
	?>
	<a href="../profile/profile_display.php?id=<?=base64_encode($l_creatingUser->getId()) ?>"><?=$l_creatingUser->getFullName() ?></a>
</td>
<td>
<b>Allocated</b>: <?=$p_action->getAllocated() ?><br/>
<b>To</b>:
	<?php
		$l_allocatedUser=$p_action->getAllocatedUser_id_Ref();
		if(isset($l_allocatedUser)){
	?>
		<a href="../profile/profile_display.php?id=<?=base64_encode($l_allocatedUser->getId()) ?>"><?=$l_allocatedUser->getFullName() ?></a>
	<?php
		}
	?>
</td>
</tr>
<?php
	$l_comments=$p_action->getComments();
	if(count($l_comments)!=0){
?>
	<tr>
	<td colspan="2">
	<br/>
	<b>Comments:</b>
	<?php
		$l_commentsTable=new TableData("ActionComments");
		$l_commentsTable->setRecords($l_comments);
		$l_commentsTable->addHeader("creatingUserFullName","By");
		$l_commentsTable->addHeader("note","Comment");
		displayTable($l_commentsTable);
	?>
	</td>
	</tr>
<?php
	}
?>
</table>
<?
}


/**
 * Display an HTML representation of an activity for reporting purpose.<br/>
 * This display should contain:
 * <ul>
 * <li>A description of the activity</li>
 * <li>The last report (in date) attached to this activity</li>
 * <li>The opened actions associated with this activity</li>
 * </ul>
 * @param $p_activity Activity an Activity instance
 * @param $p_displayLinks boolean true if the "action" links must be displayed on the tables
 */
function displayActivityDashboard($p_activity, $p_displayLinks){
?>
<h2>Description</h2>
<p><b>In charge: </b>
<?

$l_responsibleUser=$p_activity->getResponsibleUser_Id_Ref();
if(isset($l_responsibleUser)){
	echo $l_responsibleUser->getFullName();
}else{
	echo "Not yet allocated";
}

?>
</p>
<div style="border:1px solid;background-color: white;"><?=$p_activity->getDescription() ?></div>
<br/>
<h2>Last report</h2>
<?php
$l_reportItems=Item::getFromCustomCriteria("WHERE Activity_Id=:Activity_Id AND ItemType_id=:ItemType_id AND ItemStatus_id=:ItemStatus_id group by Activity_id having min(created)",
array(
	"Activity_Id" => $p_activity->getId(),
	"ItemType_id" => Item::REPORTITEM_TYPE,
 	"ItemStatus_id" => Item::REPORTITEM_STATUS_CREATED));
if(count($l_reportItems)==1){
	$l_reportItem=array_shift($l_reportItems);
	?>
	<p>
	<b>Published:</b> <?=$l_reportItem->getCreated() ?><br/>
	<b>By:</b> <?=$l_reportItem->getCreatingUser_Id_Ref()->getFullName() ?>
	</p>
	<div style="border:1px solid;background-color: white;"><?=$l_reportItem->getDescription() ?></div>
	<?php
}else{
	echo "Not report available for this activity";
}
?>
<br/>
<?php
$l_openedActions=Item::getFromCustomCriteria("WHERE Activity_Id=:Activity_Id AND ItemType_id=:ItemType_id AND ItemStatus_id=:ItemStatus_id",
array(
	"Activity_Id" => $p_activity->getId(),
	"ItemType_id" => Item::ACTION_TYPE,
 	"ItemStatus_id" => Item::ACTION_STATUS_OPENED));

?>
<h2>Opened actions</h2>
There are <b><?=count($l_openedActions) ?> opened actions</b> for this activity.
<ul>
<?php
if(count($l_openedActions) !=0){
	$l_openedActionsTable=new TableData("OpenedActionsTable");
	$l_openedActionsTable->addHeader("title","Action");
	$l_openedActionsTable->addHeader("description","Description");
	$l_openedActionsTable->addHeader("activityLabel","Activity");
	$l_openedActionsTable->addHeader("overdueHtmlFlag","Overdue");
	$l_openedActionsTable->addHeader("commentCount","Comments");
	$l_openedActionsTable->addHeader("allocated","Allocated");
	if($p_displayLinks){
		$l_openedActionsTable->addAction("addComment");
		$l_openedActionsTable->addAction("display");
	}
	$l_openedActionsTable->setRecords($l_openedActions);
	
	displayTable($l_openedActionsTable);
}
?>
</ul>
<?php
}

/**
 * Send an e-mail notifying a user that he/she was allocated with an action.
 * @param string $p_email the user e-mail address
 * @param Item $p_actionItem the Item object
 */
function notifyNewAction($p_email, $p_actionItem){
	//Action URL
	$l_rootPath="https://" . $_SERVER["SERVER_NAME"] . ROOT_PATH;
	$l_actionUrl=$l_rootPath ."/apps/user/dashboard/display_action.php?id=" . base64_encode($p_actionItem->getId());
	// subject
	$l_subject = "New action allocated by " . $p_actionItem->getCreatingUser_Id_Ref()->getFullName();
	
// message
$l_messageHtml=<<<EOT
<html>
<head>
  <title>New action</title>
</head>
<body>
<b>Please do not reply to this e-mail.</b>
<br/>
<p>You have been allocated with a new action titled:<br/>
<b>{$p_actionItem->getTitle()}</b>
</p>
Click <a href="$l_actionUrl">here</a> to display the action.
</body>
</html>
EOT;

$l_messageText=<<<EOT
Please do not reply to this e-mail.
You have been allocated with a new action titled: {$p_actionItem->getTitle()}.

Click on the specified link to display the action: {$l_actionUrl}.
EOT;

sendNotificationEmail($l_messageText, $l_messageHtml, $l_subject, $p_email);
}

/**
 * Send an e-mail notifying a user than a comment was added to an action
 * he/she is allocated.
 * @param string $p_email the user e-mail address
 * @param string $p_senderFullName the full name of the user who submitted the comment
 * @param string $p_actionId the ID of the action to which the comment has been added
 * @param Comment $p_comment the Comment object
 */
function notifyNewComment($p_email, $p_senderFullName, $p_actionId, $p_comment){
	//Action URL
	$l_rootPath="https://" . $_SERVER["SERVER_NAME"] . ROOT_PATH;
	$l_actionUrl=$l_rootPath ."/apps/user/dashboard/display_action.php?id=" . base64_encode($p_actionId);
	// subject
	$l_subject = "New comment added by " . $p_senderFullName;
	
// message
$l_messageHtml=<<<EOT
<html>
<head>
  <title>New comment</title>
</head>
<body>
<b>Please do not reply to this e-mail.</b>
<br/>
<p>A new comment has been added by {$p_senderFullName} to an action that you have been allocated.
</p>
Click <a href="$l_actionUrl">here</a> to display the action.
</body>
</html>
EOT;

$l_messageText=<<<EOT
Please do not reply to this e-mail.
A new comment has been added by {$p_senderFullName} to an action that you have been allocated.

Click on the specified link to display the action: {$l_actionUrl}.
EOT;

sendNotificationEmail($l_messageText, $l_messageHtml, $l_subject, $p_email);
}

/**
 * Send an e-mail using Mail_mime.
 * @param string $p_messageText the text mail message
 * @param string $p_messageHtml the HTML mail message
 * @param string $p_subject the mail subject
 * @param string $p_email the mail "To" address
 */
function sendNotificationEmail($p_messageText, $p_messageHtml, $p_subject, $p_email){
	$l_crlf = "\n";
	$l_headers = array(
		"From"    => "noreply@noreply",
		"Subject" => $p_subject
	);
	
	$l_mime = new Mail_mime($l_crlf);
	$l_mime->setTXTBody($p_messageText);
	$l_mime->setHTMLBody($p_messageHtml);
	
	$l_body = $l_mime->get();
	$l_headers = $l_mime->headers($l_headers);
	
	$l_mail=Mail::factory("mail");
	$l_mail->send($p_email, $l_headers, $l_body);
}

?>